/*
 * @Author: zhangyang
 * @Date: 2024-05-28 11:19:10
 * @LastEditTime: 2024-06-04 17:59:25
 * @Description: 带有 tabbar 的布局
 * @LastEditors: zhangyang
 * ██████╗ ██╗     ██╗   ██╗███████╗███████╗██╗   ██╗ ██████╗ ██╗   ██╗███╗   ██╗ ██████╗               ██╗    ██╗███████╗██████╗ 
 * ██╔══██╗██║     ██║   ██║██╔════╝██╔════╝╚██╗ ██╔╝██╔═══██╗██║   ██║████╗  ██║██╔════╝               ██║    ██║██╔════╝██╔══██╗
 * ██████╔╝██║     ██║   ██║█████╗  ███████╗ ╚████╔╝ ██║   ██║██║   ██║██╔██╗ ██║██║  ███╗    █████╗    ██║ █╗ ██║█████╗  ██████╔╝
 * ██╔══██╗██║     ██║   ██║██╔══╝  ╚════██║  ╚██╔╝  ██║   ██║██║   ██║██║╚██╗██║██║   ██║    ╚════╝    ██║███╗██║██╔══╝  ██╔══██╗
 * ██████╔╝███████╗╚██████╔╝███████╗███████║   ██║   ╚██████╔╝╚██████╔╝██║ ╚████║╚██████╔╝              ╚███╔███╔╝███████╗██████╔╝
 * ╚═════╝ ╚══════╝ ╚═════╝ ╚══════╝╚══════╝   ╚═╝    ╚═════╝  ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝                ╚══╝╚══╝ ╚══════╝╚═════╝
 * Copyright (c) 2024 to current by BluesYoung-web, All Rights Reserved. 
 */
import 'package:app_links/app_links.dart';
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '/routers/fluro_navigator.dart';
import '/routers/routers.dart';
import '/widgets/young/text_title.dart';
import '/widgets/young/dashboard.dart';

final _appLinks = AppLinks();

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State {
  final GlobalKey<YoungDashboardState> dashboardKey =
      GlobalKey<YoungDashboardState>();

  @override
  void initState() {
    super.initState();
    initSchemeHandler();
  }

  void initSchemeHandler() {
    _appLinks.uriLinkStream.listen((Uri? uri) {
      // 处理来自deep link的链接
      debugPrint('===============scheme uri===============');
      debugPrint(uri.toString());
      debugPrint('===============scheme uri===============');

      if (uri != null && uri.host == 'jumppage') {
        final jumppage = uri.path;
        final args = uri.queryParameters;
        debugPrint('jumppage: $jumppage');
        debugPrint('args: $args');

        NavigatorUtils.pushResult(context, jumppage, (_) {
          debugPrint('============scheme back============');
          // refresh page
          dashboardKey.currentState!.getBillList();
        }, arguments: args);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const TDNavBar(
        useDefaultBack: false,
        height: double.minPositive,
      ),
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 14),
        width: double.infinity,
        alignment: Alignment.topLeft,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const YoungTextTitle(title: '账本'),
            YoungDashboard(
              key: dashboardKey,
            ),
          ],
        ),
      ),
      floatingActionButton: TDButton(
        shape: TDButtonShape.circle,
        padding: const EdgeInsets.all(8),
        type: TDButtonType.fill,
        theme: TDButtonTheme.primary,
        size: TDButtonSize.large,
        child: const Icon(
          TDIcons.add,
          color: Colors.white,
        ),
        onTap: () {
          NavigatorUtils.pushResult(context, Routes.billCreate, (_) {
            debugPrint('============bill created============');
            // refresh page
            dashboardKey.currentState!.getBillList();
          });
        },
      ),
    );
  }
}
